<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_Enrollment extends MY_Model 
{

	private $enrollment_id;
	private $profile_id;
	private $level_id;
	private $student_id;


	public function enroll($data, $e_id, $p_id)
	{
		$r = new stdClass;
		$r->code = 'e';
		$r->status = false;
		$r->msg = "Enrollment Failed";
		//vd($data);
		$this->level_id = $data['grade_level'];

		if($this->save_profile($p_id, $e_id))
		{
			if($this->enroll_new_student($data,$e_id))
			{
				/*$this->load->model('M_users');
				$this->M_users->create_student_login($this->profile_id, $this->enrollment_id);*/

				if($this->generate_fees($this->level_id,$this->enrollment_id))
				{
					if($this->_check_enrollee_nationality($this->level_id,$this->enrollment_id))
					{
						$r->status = TRUE;
						$r->code = "s";
						$r->msg = "Your child's enrollment was successfully submitted to the registrar for approval."; //enroll success
					}else{
						$r->msg = "Unable to generate foreign fees."; //unable to generate foreign fees
					}
				}else{
					$r->msg = "Unable to generate fees for student."; //unable to generate fees
				}
			}else{
				$r->msg = "Unable to save enrollment."; //unable to save in enrollment table
			}
		}else{
			$r->msg = "Error occured unable to save profile."; //unable to save profile
		}

		return $r;
	}

	private function save_profile($p_id, $e_id)
	{
		//check profile if exist
		$q = $this->db->query("SELECT profile_id FROM profiles WHERE profile_id = ?",[$p_id]);
		if($q->num_rows() <= 0){
			return FALSE;
		}
		$this->profile_id = $p_id;

		//check enrollment if exist
		$this->load->model('M_enrollees');
		$en = $this->M_enrollees->get_enrollee_prof($e_id);
		$this->student_id = $en->idno;
		if(!$en){
			return FALSE;
		}

		return TRUE;
	}

	private function enroll_new_student($data)
	{
		$CI =& get_instance();
		$CI->load->library('idno_creator');
		$generated_studentid = $CI->idno_creator->generate_new_id_compressed($this->level_id);

		$sy = $this->o_sy;
		$enrollment['e_sy_from'] = $sy->sy_from;
		$enrollment['e_sy_to'] = $sy->sy_to;
		$enrollment['e_ay_id'] = $sy->id;
		$enrollment['e_profile_id'] = $this->profile_id;
		$enrollment['e_grade_level'] = $this->level_id;
		$enrollment['e_section_id'] = $data['section'] == "noblock" ? '0' : $data['section'];
		$enrollment['e_created_at'] = NOW;
		$enrollment['verified'] = 1;
		$enrollment['registrar_approved'] = 1;
		$enrollment['unfinished'] = 0;
		$enrollment['e_student_id'] = $this->student_id;
		$enrollment['e_enrollee_type'] = "old";
		
		$this->db->insert('enrollments',$enrollment);
		$this->enrollment_id = $this->db->insert_id();
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;	
	}

	private function generate_fees($level_id,$enroll_id)
	{
		$l = $this->db->select('payment_group')->limit(1)->where('level_id',$level_id)->get('levels');
	
		if($l->num_rows() >= 1)
		{
			$payment_group_id = $l->row()->payment_group;
			if($payment_group_id !== 0)
			{
				$select = $this->db->where('group_id',$payment_group_id)
							       ->get('payment_group_fees');
			
				
				if($select->num_rows() >= 1)
				{
					foreach($select->result() as $k => $v)
					{
						if($v->fee_type == 'misc')
						{
							$student_fee[$k]['is_misc_fee'] = 1;
							$student_fee[$k]['is_cca_fee'] = 0;
							$student_fee[$k]['is_tuition_fee'] = 0;
						}elseif($v->fee_type == 'tuit')
						{
							$student_fee[$k]['is_tuition_fee'] = 1;
							$student_fee[$k]['is_cca_fee'] = 0;
							$student_fee[$k]['is_misc_fee'] = 0;
						
						}elseif($v->fee_type == 'cca')
						{
							$student_fee[$k]['is_cca_fee'] = 1;
							$student_fee[$k]['is_misc_fee'] = 0;
							$student_fee[$k]['is_tuition_fee'] = 0;
						}
						
						
						$student_fee[$k]['sef_enrollment_id'] = $enroll_id;
						$student_fee[$k]['sef_fee_name'] = $v->fee_name;
						$student_fee[$k]['sef_fee_rate'] = $v->fee_price;
						$student_fee[$k]['sef_schoolyear_id'] = $this->c_sy->id;
						$student_fee[$k]['sef_gperiod_id'] = $this->c_gp->gp_id;
						$student_fee[$k]['sef_created'] = NOW;

					}

						$this->db->insert_batch('student_enrollment_fees',$student_fee);
						return $this->db->affected_rows() > 0 ? TRUE : FALSE;	
				
				}else{
					return TRUE;
				}
			}else{
				return TRUE;
			}
		}else{
			return TRUE;
		}
	}

	/*check if foreigner then apply necessary fees*/
	private function _check_enrollee_nationality($lid,$eid)
	{
		// get enrolee nationality
		$q = $this->db->select('nationality')
				 ->join('enrollments e','e.e_profile_id = p.profile_id')
				 ->where('e_id',$eid)
				 ->limit(1)
				 ->get('profiles p');
		
		
		if($q->num_rows() >=1 )
		{
			$nat= $q->row()->nationality;		
				
			//if nationality is filipino no foreign fee applied
			if(strtolower($nat) == 'filipino')
			{
				return TRUE;
			}else{
				//get and insert foreign fees
				$ff = 'INSERT INTO student_enrollment_fees (sef_enrollment_id,
															sef_fee_name,
															sef_fee_rate,
															sef_gperiod_id,
															sef_schoolyear_id,
															is_elective_fee
															)
						SELECT ?,ff.fee_name,ff.fee_rate,?,?,1
						FROM foreign_fee ff
						WHERE ff.level_id = ?';
				
				$this->db->query($ff,array($eid,$this->c_gp->gp_id,$this->c_sy->id,$lid));
				
				return TRUE;
			}
		}else{
			return TRUE;
		}
	}
}

/* End of file m_Enrollment.php */
/* Location: ./application/models/m_Enrollment.php */